package com.example.administrator.newsdemo.cn.gdcp.fragments;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.newsdemo.ContentActivity;
import com.example.administrator.newsdemo.MainActivity;
import com.example.administrator.newsdemo.NewsBean;
import com.example.administrator.newsdemo.R;

import java.util.List;

/**
 * Created by Administrator on 2018/5/7 0007.
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<NewsBean.ResultBean.DataBean> dataBeans;
    private Context context;
    static class ViewHolder extends RecyclerView.ViewHolder{
        View itemView;
        TextView tv_title;
        TextView tv_author;
        ImageView img_head;
        public ViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_author = itemView.findViewById(R.id.tv_author);
            img_head = itemView.findViewById(R.id.img_header);
        }
    }

    public MyAdapter(List<NewsBean.ResultBean.DataBean> dataBeans){
        this.dataBeans = dataBeans;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view,null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final NewsBean.ResultBean.DataBean dataBean = dataBeans.get(position);
        holder.tv_title.setText(dataBean.getTitle());
        holder.tv_author.setText(dataBean.getAuthor_name());
        Glide.with(context).load(dataBean.getThumbnail_pic_s()).into(holder.img_head);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ContentActivity.class);
                intent.putExtra("URL",dataBean.getUrl());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataBeans.size();
    }

}
